from django.db import models
from utils.model import BaseModel
from django.contrib.auth.models import AbstractUser


# Create your models here.

class User(AbstractUser, BaseModel):
    """查看交易密码"""
    mobile = models.CharField(max_length=20, null=True, blank=True, verbose_name='手机')
    # 1-普通用户 2-财务人员 3-总经理
    user_type = models.IntegerField(default=1, verbose_name='用户身份')
    status = models.SmallIntegerField(default=1, verbose_name="状态")

    class Meta:
        db_table = 'js_user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name


class Agent(BaseModel):
    """身份标识"""
    customer_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='代理名称')
    customer_id = models.CharField(max_length=50, null=True, blank=True, verbose_name='客户编号')
    mobile = models.CharField(max_length=50, null=True, blank=True, verbose_name="手机号")
    # 0-巨石客户 1-全电客户
    type = models.SmallIntegerField(default=0, verbose_name="客户类型")
    status = models.SmallIntegerField(default=1, verbose_name="状态")

    class Meta:
        db_table = 'js_agent'
        verbose_name = '身份标识'
        verbose_name_plural = verbose_name


class Trade(BaseModel):
    """交易明细"""
    date = models.DateField(null=True, verbose_name="交易日期")
    price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="金额", default=0)
    equipment = models.CharField(max_length=100, default='', verbose_name="设备")
    count = models.IntegerField(default=0, verbose_name="总数量")
    no_deliver_count = models.IntegerField(default=0, verbose_name="未发货数量")
    no_cost = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="未发成本")
    generated_cost = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="已发成本")
    collection = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="已收款")
    no_collection = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="未收款")
    profits = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="利润")
    out_fare = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="发货运费")
    in_fare = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="退货运费")
    generated_commission = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="已发提成")
    no_commission = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="未发提成")
    agent = models.ForeignKey(Agent, null=True, on_delete=models.CASCADE, related_name='agent')
    # 0-已删除 1-有效 2-已拒绝
    status = models.SmallIntegerField(default=1, verbose_name="状态")
    finance_check = models.BooleanField(default=False, verbose_name="财务审核")
    manager_check = models.BooleanField(default=False, verbose_name="总经理审核")

    class Meta:
        db_table = 'js_trade'
        verbose_name = '交易明细'
        verbose_name_plural = verbose_name


class CostDetails(BaseModel):
    """成本价目表"""
    date = models.DateField(null=True, verbose_name="日期")
    eight_cabinet = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="8口机柜")
    sixteen_cabinet = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="16口机柜-展架式")
    sixteen_cabinet_oneBody = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="16口机柜-一体式")
    thirty_two_cabinet = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="32口机柜")
    forty_eight_cabinet = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="48口机柜")
    power_bank = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="充电宝")
    status = models.SmallIntegerField(default=1, verbose_name="状态")

    class Meta:
        db_table = 'js_cost'
        verbose_name = '成本价目表'
        verbose_name_plural = verbose_name


class SentTradeDetails(BaseModel):
    """发货详情"""
    date = models.DateField(null=True, verbose_name="发货日期")
    equipment = models.CharField(max_length=100, default='', verbose_name="设备")
    count = models.IntegerField(default=0, verbose_name="数量")
    sent = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="发货运费")
    cost = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="发货成本")
    trade = models.ForeignKey(Trade, null=True, on_delete=models.CASCADE, related_name='trade_sent')
    remark = models.CharField(max_length=50, null=True, blank=True, verbose_name='备注')
    status = models.SmallIntegerField(default=1, verbose_name="状态")

    class Meta:
        db_table = 'js_sent'
        verbose_name = '交易详情'
        verbose_name_plural = verbose_name


class ReturnTradeDetails(BaseModel):
    """退货详情"""
    date = models.DateField(null=True, verbose_name="退货日期")
    equipment = models.CharField(max_length=100, default='', verbose_name="设备")
    count = models.IntegerField(default=0, verbose_name="数量")
    return_fare = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="退货运费")
    cost = models.DecimalField(max_digits=8, decimal_places=2, default=0, verbose_name="退货成本")
    trade = models.ForeignKey(Trade, null=True, on_delete=models.CASCADE, related_name='trade_return')
    remark = models.CharField(max_length=40, null=True, blank=True, verbose_name="备注")
    classes = models.CharField(max_length=5, null=True, blank=True, verbose_name="退货类型")
    status = models.SmallIntegerField(default=1, verbose_name="状态")

    class Meta:
        db_table = 'js_return'
        verbose_name = '交易详情'
        verbose_name_plural = verbose_name
